import Exif from 'exif-js';
import Vue from 'vue'
// import { Notify } from 'vant';
/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, ' ') +
        '"}'
    )
}

export function deepClone(source) {
    if (!source && typeof source !== 'object') {
        throw new Error('error arguments', 'deepClone')
    }
    const targetObj = source.constructor === Array ? [] : {}
    Object.keys(source).forEach(keys => {
        if (source[keys] && typeof source[keys] === 'object') {
            targetObj[keys] = deepClone(source[keys])
        } else {
            targetObj[keys] = source[keys]
        }
    })
    return targetObj
}

//获取当前设备为移动端还是pc端
export const detectDeviceType = () => {
    return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? 'Mobile' : 'Desktop';
}

export const currentURL = () => window.location.href

/**
 * @desc 获取图片信息，使用exif.js库，具体用法请在github中搜索
 * @param {Object} file 上传的图片文件
 * @param {String} tag 需要获取的信息 例如：'Orientation'旋转信息
 * @return {Promise<Any>} 读取是个异步操作，返回指定的图片信息
 */
export const getOrientation = (file, tag) => {
    if (!file) return 0;
    return new Promise((resolve, reject) => {
        /* eslint-disable func-names */
        // 箭头函数会修改this，所以这里不能用箭头函数
        Exif.getData(file, function() {
            const o = Exif.getTag(this, tag);
            resolve(o);
        });
    });
};

/**
 * @desc 将base64的图片转为文件流
 * @param {String} dataUrl base64数据
 * @return {Object} 文件流
 */
export const dataURLtoFile = (dataUrl) => {
    const filename = `img${Date.now()}`;
    const arr = dataUrl.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, { type: mime });
};

/**
 * @desc 旋转canvas，会对源数据canvas进行修改
 * @param {Object} img 图片文件
 * @param {String} dir 方向 left逆时针|right顺时针
 * @param {Object} canvas 画布
 * @param {Number} s 向指定方向旋转几步，1步为90度
 */
export const rotateImg = (img, dir = 'right', canvas, s = 1) => {
    const MIN_STEP = 0;
    const MAX_STEP = 3;

    const width = canvas.width || img.width;
    const height = canvas.height || img.height;
    let step = 0;

    if (dir === 'right') {
        step += s;
        step > MAX_STEP && (step = MIN_STEP);
    } else {
        step -= s;
        step < MIN_STEP && (step = MAX_STEP);
    }

    const degree = step * 90 * Math.PI / 180;
    const ctx = canvas.getContext('2d');

    switch (step) {
        case 1:
            canvas.width = height;
            canvas.height = width;
            ctx.rotate(degree);
            ctx.drawImage(img, 0, -height, width, height);
            break;
        case 2:
            canvas.width = width;
            canvas.height = height;
            ctx.rotate(degree);
            ctx.drawImage(img, -width, -height, width, height);
            break;
        case 3:
            canvas.width = height;
            canvas.height = width;
            ctx.rotate(degree);
            ctx.drawImage(img, -width, 0, width, height);
            break;
        default:
            canvas.width = width;
            canvas.height = height;
            ctx.drawImage(img, 0, 0, width, height);
            break;
    }
};

/**
 * @desc 压缩canvas，会对源数据canvas进行修改
 * @param {Object} img 图片文件
 * @param {Object} canvas 画布上下文
 * @param {Number} limitsize 限制的大小
 * @return {Object} 新大小的宽高，因为如果后续还要处理canvas要使用新的宽高才行{width, height}
 */
export const compressImg = (img, canvas, limitsize) => {
    if (!limitsize || limitsize <= 0) return;
    const ctx = canvas.getContext('2d');

    // 原始图片信息
    const imgSize = img.src.length;
    const width = img.width;
    const height = img.height;

    // 压缩比, 保留两位小数
    const ratio = parseFloat(limitsize / imgSize).toFixed(2);
    const ratioWidth = ~~(width * ratio); // 新图片宽度
    const ratioHeight = ~~(height * ratio); // 新图片高度

    canvas.width = ratioWidth;
    canvas.height = ratioHeight;
    ctx.drawImage(img, 0, 0, ratioWidth, ratioHeight);
};


export const debounce = (fn, time = 500) => {
    let timeout = null;
    return function() {
        clearTimeout(timeout)
        timeout = setTimeout(() => {
            fn.apply(this, arguments);
        }, time);
    }
};
// 手机号隐私处理
export const hidePhone = (str) => {
    return str.substr(0, 3) + "****" + str.substr(7);
}
export const convertNum = (val) => {
    let tmpnewchar = ''
    let nums = val + '';
    let array = nums.split("");
    let N = [
        "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"
    ];
    let T = ["十", "百", "千", "万", ];
    // 扩展到百位
    switch (array.length) {
        case 1:
            tmpnewchar = N[Number(array[array.length - 1])];
            break;
        case 2:
            if (Number(array[0]) == 1 && Number(array[1]) == 0) {
                tmpnewchar = T[array.length - 2];
            } else if (Number(array[0]) == 1 && Number(array[1]) != 0) {
                tmpnewchar = T[array.length - 2] + N[Number(array[1])];
            } else if (Number(array[0]) != 1 && Number(array[1]) == 0) {
                tmpnewchar = N[Number(array[0])] + T[array.length - 2];
            } else {
                tmpnewchar = N[Number(array[0])] + T[array.length - 2] + N[Number(array[1])];
            }
            break;
        case 3:
            if (Number(array[1]) == 0 && Number(array[2]) == 0) {
                tmpnewchar = N[Number(array[0])] + T[array.length - 2]
            } else if (Number(array[1]) == 0) {
                tmpnewchar = N[Number(array[0])] + T[array.length - 2] + N[Number(array[1])] + N[Number(array[2])];
            } else if (Number(array[2]) == 0) {
                tmpnewchar = N[Number(array[0])] + T[array.length - 2] + N[Number(array[1])] + T[array.length - 3] + N[Number(array[2])];
            } else {
                tmpnewchar = N[Number(array[0])] + T[array.length - 2] + N[Number(array[1])] + T[array.length - 3] + N[Number(array[2])];
            }
            break;

        default:
            break;
    }
    return tmpnewchar
}
export function getIsWxClient() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return 'wx';
    }
    if (window.navigator.userAgent.includes("DingTalk")) {
        return 'dd';
    }
    if (ua.match(/WeiBo/i) == "Weibo") {　　　　 // 在新浪微博客户端打开
        return 'Weibo'　　
    }　　
    if (ua.match(/QQ/i) == "QQ") {　　　　 // 在QQ端打开
        return 'QQ'　　
    }
    return false;
};
export const down = Vue.directive('down', {
    inserted: (el, binding) => {
        el.style.cssText = 'cursor: pointer;color:write;'
        el.addEventListener('click', () => {
            console.log(binding.value)
            let link = document.createElement('a')
            let url = binding.value
            if (getIsWxClient()) {
                link.href = url
                console.log(link.href)
                link.download = ''
                document.body.appendChild(link)
                link.click()
            } else {
                // 这里是将url转成blob地址，
                fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
                    link.href = URL.createObjectURL(blob)
                    console.log(link.href)
                    link.download = ''
                    document.body.appendChild(link)
                    link.click()
                })
            }

        })
    }
})